<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="" prop="monthly">
        <!-- <el-input
          v-model="queryParams.monthly"
          placeholder="请输入月份"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-date-picker
          v-model="queryParams.monthly"
          type="month"
          placeholder="选择月"
          @change="changeMonth"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="warehouseInOutOrderNum">
        <el-input
          v-model="queryParams.warehouseInOutOrderNum"
          placeholder="请输入进出仓单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="purchaseOrderNumber">
        <el-input
          v-model="queryParams.purchaseOrderNumber"
          placeholder="请输入采购单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="purchaseDate">
        <el-date-picker
          clearable
          v-model="queryParams.purchaseDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择采购日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="warehouseEntryTime">
        <el-date-picker
          clearable
          v-model="queryParams.warehouseEntryTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择入仓时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入商品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="出入库数量" prop="inventoryQuantity">
        <el-input
          v-model="queryParams.inventoryQuantity"
          placeholder="请输入出入库数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="进出仓金额(含税)" prop="inOutWarehouseAmount">
        <el-input
          v-model="queryParams.inOutWarehouseAmount"
          placeholder="请输入进出仓金额(含税)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="外部单号" prop="externalOrderNumber">
        <el-input
          v-model="queryParams.externalOrderNumber"
          placeholder="请输入外部单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="数据-年" prop="dataYear">
        <el-input
          v-model="queryParams.dataYear"
          placeholder="请输入数据-年"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据-月" prop="dataMonth">
        <el-input
          v-model="queryParams.dataMonth"
          placeholder="请输入数据-月"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['financial:BiInOutWarehouse:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['financial:BiInOutWarehouse:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['financial:BiInOutWarehouse:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['financial:BiInOutWarehouse:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button size="mini" plain icon="el-icon-upload" @click="daoru"
          >导入</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="BiInOutWarehouseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="进出仓数据id" align="center" prop="id" />
      <el-table-column label="月份" align="center" prop="monthly" />
      <el-table-column
        label="进出仓单号"
        align="center"
        prop="warehouseInOutOrderNum"
      />
      <el-table-column
        label="采购单号"
        align="center"
        prop="purchaseOrderNumber"
      />
      <el-table-column label="单据类型" align="center" prop="documentType" />
      <el-table-column
        label="采购日期"
        align="center"
        prop="purchaseDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.purchaseDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="入仓时间"
        align="center"
        prop="warehouseEntryTime"
      >
      </el-table-column>
      <el-table-column
        width="150"
        label="商品编号"
        align="center"
        prop="productCode"
      />
      <el-table-column
        label="出入库数量"
        align="center"
        prop="inventoryQuantity"
      />
      <el-table-column
        label="进出仓金额(含税)"
        align="center"
        prop="inOutWarehouseAmount"
      />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column
        label="外部单号"
        align="center"
        prop="externalOrderNumber"
      />
      <el-table-column label="数据-年" align="center" prop="dataYear" />
      <el-table-column label="数据-月" align="center" prop="dataMonth" />

      <el-table-column
        label="预约单号"
        align="center"
        prop="reservationOrderNumber"
      />
      <el-table-column label="款式编码" align="center" prop="productNo" />
      <el-table-column label="商品标签" align="center" prop="productTag" />
      <el-table-column
        label="商品名称(单据)"
        align="center"
        prop="productNameDocument"
      />
      <el-table-column
        label="颜色规格"
        align="center"
        prop="colorSpecification"
      />
      <el-table-column
        label="采购单"
        align="center"
        prop="labelPurchaseOrder"
      />
      <el-table-column
        label="采购入库/采购退货"
        align="center"
        prop="labelPurchaseWarehouseReturn"
      />
      <el-table-column label="单价" align="center" prop="unitPrice" />
      <el-table-column
        label="预计到货时间"
        align="center"
        prop="expectedDeliveryTime"
      />
      <el-table-column
        label="预计到货数量"
        align="center"
        prop="expectedDeliveryQuantity"
      />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="分类" align="center" prop="category" />
      <el-table-column label="虚拟分类" align="center" prop="virtualCategory" />
      <el-table-column
        label="生产批次号"
        align="center"
        prop="productionBatchNumber"
      />
      <el-table-column label="生产日期" align="center" prop="productionDate" />
      <el-table-column label="仓库" align="center" prop="warehouse" />
      <el-table-column
        label="供应商商品款号"
        align="center"
        prop="supplierProductStyleNumber"
      />
      <el-table-column
        label="供应商商品编码"
        align="center"
        prop="supplierProductCode"
      />
      <el-table-column
        label="进出仓单价(不含税)"
        align="center"
        prop="inOutWarehouseUnitPriceExcludeTax"
      />
      <el-table-column
        label="进出仓金额(不含税)"
        align="center"
        prop="inOutWarehouseAmountExcludeTax"
      />
      <el-table-column label="明细行备注" align="center" prop="detailRemarks" />
      <el-table-column
        label="基本售价"
        align="center"
        prop="baseSellingPrice"
      />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['financial:BiInOutWarehouse:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['financial:BiInOutWarehouse:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改进出仓数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="月份" prop="monthly">
          <el-input v-model="form.monthly" placeholder="请输入月份" />
        </el-form-item>
        <el-form-item label="进出仓单号" prop="warehouseInOutOrderNum">
          <el-input
            v-model="form.warehouseInOutOrderNum"
            placeholder="请输入进出仓单号"
          />
        </el-form-item>
        <el-form-item label="采购单号" prop="purchaseOrderNumber">
          <el-input
            v-model="form.purchaseOrderNumber"
            placeholder="请输入采购单号"
          />
        </el-form-item>
        <el-form-item label="采购日期" prop="purchaseDate">
          <el-date-picker
            clearable
            v-model="form.purchaseDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择采购日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入仓时间" prop="warehouseEntryTime">
          <el-date-picker
            clearable
            v-model="form.warehouseEntryTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入仓时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="商品编号" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入商品编号" />
        </el-form-item>
        <el-form-item label="出入库数量" prop="inventoryQuantity">
          <el-input
            v-model="form.inventoryQuantity"
            placeholder="请输入出入库数量"
          />
        </el-form-item>
        <el-form-item label="进出仓金额(含税)" prop="inOutWarehouseAmount">
          <el-input
            v-model="form.inOutWarehouseAmount"
            placeholder="请输入进出仓金额(含税)"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input
            v-model="form.remarks"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="外部单号" prop="externalOrderNumber">
          <el-input
            v-model="form.externalOrderNumber"
            placeholder="请输入外部单号"
          />
        </el-form-item>
        <el-form-item label="数据-年" prop="dataYear">
          <el-input v-model="form.dataYear" placeholder="请输入数据-年" />
        </el-form-item>
        <el-form-item label="数据-月" prop="dataMonth">
          <el-input v-model="form.dataMonth" placeholder="请输入数据-月" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改产品二级信息对话框 :on-change="handleChange"-->
    <el-dialog
      title="导入"
      :visible.sync="dialogVisible"
      width="500px"
      append-to-body
      v-if="dialogVisible"
    >
      <!-- 文件上传-->
      <el-form
        :rules="rules"
        :model="dataForm"
        ref="dataForm"
        label-width="150px"
        @submit.native.prevent
      >
        <el-form-item label="" prop="name">
          <el-date-picker
            v-model="dataForm.name"
            type="month"
            placeholder="选择月"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="文件：" prop="file">
          <el-upload
            class="upload-import"
            ref="uploadImport"
            :http-request="httpRequest"
            action=""
            :on-remove="handleRemove"
            :file-list="fileList"
            :limit="1"
            :on-change="handleChange"
            :auto-upload="false"
            :before-upload="beforeAvatarUpload"
          >
            <!--
            accept="application/zip,.zip" 设置接收的文件类型-->
            <el-button
              v-show="!hasFile"
              slot="trigger"
              size="small"
              type="primary"
              >选取文件</el-button
            >
            <!-- <div slot="tip" class="el-upload__tip">
          只能上传zip文件，且不超过10M
        </div> -->
          </el-upload>
          <!-- 关闭自动上传-->
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitUpload">提交</el-button>
          <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
          <el-button @click="dialogVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBiInOutWarehouse,
  getBiInOutWarehouse,
  delBiInOutWarehouse,
  addBiInOutWarehouse,
  updateBiInOutWarehouse,
} from "@/api/financial/BiInOutWarehouse";
import axios from "axios";
//--------导入token
import { getToken } from "@/utils/auth";
import moment from "moment";
export default {
  name: "BiInOutWarehouse",
  data() {
    return {
      dataForm: {
        name: "",
        file: null,
      },
      hasFile: false,
      fileList: [],
      dialogVisible: false,
      pjtUrl: process.env.VUE_APP_BASE_API,
      isOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 进出仓数据表格数据
      BiInOutWarehouseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        monthly: null,
        warehouseInOutOrderNum: null,
        purchaseOrderNumber: null,
        documentType: null,
        purchaseDate: null,
        warehouseEntryTime: null,
        productCode: null,
        inventoryQuantity: null,
        inOutWarehouseAmount: null,
        remarks: null,
        externalOrderNumber: null,
        dataYear: null,
        dataMonth: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changeMonth() {
      let parts = moment(this.queryParams.monthly).format("yyyy-MM").split("-");
      this.queryParams.dataYear = parts[0];
      this.queryParams.dataMonth = parts[1];
    },
    //图片上传前的相关判断
    beforeAvatarUpload(file) {
      // const isJPG = file.type == 'image/jpeg' || file.type == 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 100;
      // if (!isJPG) {
      //     this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
      // }
      if (!isLt2M) {
        this.$message.error("上传导入文件大小不能超过 100MB!");
      }

      return isLt2M;
    },
    handleRemove(file, fileList) {
      if (!fileList.length) {
        this.hasFile = false;
      }
      this.dataForm.file = null;
    },
    handleChange(file, fileList) {
      if (fileList.length >= 2) {
        return;
      }
      if (fileList.length === 1) {
        this.hasFile = true;
      }
      this.dataForm.file = file;
    },
    //确定按钮
    //调用组件upload的submit方法，从而触发httpRequest方法，实现手动上传
    submitUpload() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$refs.uploadImport.submit();
        }
      });
    },
    httpRequest(param) {
      // if (this.dataForm.name.length < 3) {
      //   this.$modal.msgWarning("请选择到店铺");
      //   return;
      // }
      this.dataForm.name = moment(this.dataForm.name).format("YYYY-MM");
      let fd = new FormData();
      fd.append("file", param.file); // 传文件
      fd.append("dataYear", this.dataForm.name.split("-")[0]);
      fd.append("dataMonth", this.dataForm.name.split("-")[1]);
      //dataPar.file.raw
      console.log(JSON.stringify(fd));
      this.openLoading();
      axios
        .post(
          process.env.VUE_APP_BASE_API +
            "/financial/BiInOutWarehouse/importData",
          fd,
          {
            headers: {
              header: { responseType: "blob" },

              Authorization: "Bearer " + getToken(),
              "Content-Type": "multipart/form-data",
            }, //定义内容格式,很重要
            // timeout: 20000,
          }
        )
        .then((response) => {
          console.log(response);
          if (response.data.code == 200) {
            this.dialogVisible = false;
            this.$modal.closeLoading();
            this.$modal.msgSuccess("导入成功");
            this.hasFile = false;
            this.getList();
          }
        });
    },
    //部门文件导入修改打开
    daoru() {
      this.dialogVisible = true;
    },
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在导入，请稍候！");
    },
    /** 查询进出仓数据列表 */
    getList() {
      this.loading = true;
      listBiInOutWarehouse(this.queryParams).then((response) => {
        this.BiInOutWarehouseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        monthly: null,
        warehouseInOutOrderNum: null,
        purchaseOrderNumber: null,
        documentType: null,
        purchaseDate: null,
        warehouseEntryTime: null,
        productCode: null,
        inventoryQuantity: null,
        inOutWarehouseAmount: null,
        remarks: null,
        externalOrderNumber: null,
        dataYear: null,
        dataMonth: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加进出仓数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiInOutWarehouse(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改进出仓数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiInOutWarehouse(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBiInOutWarehouse(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除进出仓数据编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBiInOutWarehouse(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "financial/BiInOutWarehouse/export",
        {
          ...this.queryParams,
        },
        `BiInOutWarehouse_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
